package com.test;

import junit.framework.TestCase;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Created by Administrator on 2016/3/14.
 */
public class JMSSubscriber extends TestCase {

    public void testSubscriber() throws NamingException, InterruptedException {
        Context context = new InitialContext();
        TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("TopicConnectionFactory");
        TopicConnection connection = null;

        try {
            connection = factory.createTopicConnection();
            connection.start();

            Topic topic = (Topic) context.lookup("MyTopic");
            TopicSession session = connection.createTopicSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            TopicSubscriber subscriber1 = session.createSubscriber(topic);
            TopicSubscriber subscriber2 = session.createSubscriber(topic);

            new ConsumerMessageListener(subscriber1);
            new ConsumerMessageListener(subscriber2);

            Thread.sleep(60 * 1000);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                assert connection != null;
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }

    }

}
